package com.hzsparrow.ai.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzsparrow.ai.entity.ModelConfig;
import com.hzsparrow.ai.service.ModelConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 模型配置管理控制器
 */
@RestController
@RequestMapping("/api/model-config")
public class ModelConfigController {

    @Autowired
    private ModelConfigService modelConfigService;

    /**
     * 获取所有启用的模型配置
     */
    @GetMapping("/enabled")
    public ResponseEntity<?> getAllEnabledModels() {
        try {
            List<ModelConfig> models = modelConfigService.getAllEnabledModels();
            return ResponseEntity.ok(models);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body("获取模型配置失败: " + e.getMessage());
        }
    }

    /**
     * 根据模型类型获取模型配置
     */
    @GetMapping("/type/{modelType}")
    public ResponseEntity<?> getModelsByType(@PathVariable String modelType) {
        try {
            List<ModelConfig> models = modelConfigService.getModelsByType(modelType);
            return ResponseEntity.ok(models);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body("获取模型配置失败: " + e.getMessage());
        }
    }

    /**
     * 根据模型分类获取模型配置
     */
    @GetMapping("/category/{category}")
    public ResponseEntity<?> getModelsByCategory(@PathVariable String category) {
        try {
            List<ModelConfig> models = modelConfigService.getModelsByCategory(category);
            return ResponseEntity.ok(models);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body("获取模型配置失败: " + e.getMessage());
        }
    }

    /**
     * 分页查询模型配置列表
     */
    @GetMapping
    public ResponseEntity<?> listModelConfigs(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String category,
            @RequestParam(required = false) String modelType) {
        
        try {
            Page<ModelConfig> result = modelConfigService.listModelConfigs(page, size, category, modelType);
            
            Map<String, Object> response = new HashMap<>();
            response.put("total", result.getTotal());
            response.put("pages", result.getPages());
            response.put("current", result.getCurrent());
            response.put("size", result.getSize());
            response.put("records", result.getRecords());
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body("获取模型配置列表失败: " + e.getMessage());
        }
    }

    /**
     * 获取模型配置详情
     */
    @GetMapping("/{id}")
    public ResponseEntity<?> getModelConfig(@PathVariable Long id) {
        try {
            ModelConfig modelConfig = modelConfigService.getModelConfig(id);
            if (modelConfig == null) {
                return ResponseEntity.notFound().build();
            }
            return ResponseEntity.ok(modelConfig);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body("获取模型配置详情失败: " + e.getMessage());
        }
    }

    /**
     * 创建模型配置
     */
    @PostMapping
    public ResponseEntity<?> createModelConfig(@RequestBody ModelConfig modelConfig) {
        try {
            ModelConfig created = modelConfigService.createModelConfig(modelConfig);
            return ResponseEntity.ok(created);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body("创建模型配置失败: " + e.getMessage());
        }
    }

    /**
     * 更新模型配置
     */
    @PutMapping("/{id}")
    public ResponseEntity<?> updateModelConfig(@PathVariable Long id, @RequestBody ModelConfig modelConfig) {
        try {
            // 检查模型配置是否存在
            ModelConfig existing = modelConfigService.getModelConfig(id);
            if (existing == null) {
                return ResponseEntity.notFound().build();
            }
            
            // 设置ID
            modelConfig.setId(id);
            modelConfig.setCreateTime(existing.getCreateTime());
            
            ModelConfig updated = modelConfigService.updateModelConfig(modelConfig);
            return ResponseEntity.ok(updated);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body("更新模型配置失败: " + e.getMessage());
        }
    }

    /**
     * 删除模型配置
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<?> deleteModelConfig(@PathVariable Long id) {
        try {
            // 检查模型配置是否存在
            ModelConfig existing = modelConfigService.getModelConfig(id);
            if (existing == null) {
                return ResponseEntity.notFound().build();
            }
            
            boolean deleted = modelConfigService.deleteModelConfig(id);
            
            if (deleted) {
                return ResponseEntity.ok(Map.of("message", "模型配置删除成功"));
            } else {
                return ResponseEntity.badRequest().body("模型配置删除失败");
            }
        } catch (Exception e) {
            return ResponseEntity.badRequest().body("删除模型配置失败: " + e.getMessage());
        }
    }
} 